
*Importing data-file
use Data2015ref

sort ID

******Cleaning data before reshaping to long format******
codebook QDataCollection_StartTime  Q2 Q4 Q5 Q6_1_resp Q6_2_resp Q7 Q9 Q10 Q11 Q12 Q15 Q16 Q18_1_resp Q19_1_resp Q19_2_resp Q19_3_resp Q20_1_resp Q20_2_resp Q20_3_resp Q20_4_resp Q22_2_resp Q22_3_resp Q22_5_resp Q22_6_resp Q25_1_resp Q19_1_resp_parti PDataCollection_StartTime PDataCollection_FinishTime P6_1_resp P6_2_resp P7 P10 P11 P12 P15 P16 P18_1_resp P19_1_resp P19_2_resp P19_3_resp P20_1_resp P20_2_resp P20_3_resp P20_4_resp P22_2_resp P22_3_resp P22_5_resp P22_6_resp P19_1_resp_parti P25_1_resp alder region, tab(99) 

keep ID QDataCollection_StartTime  Q2 Q4 Q5 Q6_1_resp Q6_2_resp Q7 Q9 Q10 Q11 Q12 Q15 Q16 Q18_1_resp Q19_1_resp Q19_2_resp Q19_3_resp Q20_1_resp Q20_2_resp Q20_3_resp Q20_4_resp Q22_2_resp Q22_3_resp Q22_5_resp Q22_6_resp Q19_1_resp_parti Q25_1_resp PDataCollection_StartTime P6_1_resp P6_2_resp P7 P10 P11 P12 P15 P16 P18_1_resp P19_1_resp P19_2_resp P19_3_resp P20_1_resp P20_2_resp P20_3_resp P20_4_resp P22_2_resp P22_3_resp P22_5_resp P22_6_resp P19_1_resp_parti P25_1_resp alder region 

rename QDataCollection_StartTime interviewdate_pre
rename Q6_1_resp Q6_2_pre
rename Q6_2_resp Q6_pre
rename Q7 Q7_pre
rename Q10 Q10_pre
rename Q11 Q11_pre
rename Q12 Q12_pre
rename Q15 Q15_pre
rename Q16 Q16_pre
rename Q18_1_resp Q18_1_pre
rename Q19_1_resp Q19_1_pre
rename Q19_2_resp Q19_2_pre
rename Q19_3_resp Q19_3_pre
rename Q20_1_resp Q20_1_pre
rename Q20_2_resp Q20_2_pre
rename Q20_3_resp Q20_3_pre
rename Q20_4_resp Q20_4_pre
rename Q22_2_resp Q22_2_pre
rename Q22_3_resp Q22_3_pre
rename Q22_5_resp Q22_5_pre
rename Q22_6_resp Q22_6_pre
rename Q19_1_resp_parti Q19_party_pre
rename Q25_1_resp Q25_1_pre

rename PDataCollection_StartTime interviewdate_post
rename P6_1_resp Q6_2_post
rename P6_2_resp Q6_post
rename P7 Q7_post
rename P10 Q10_post
rename P11 Q11_post
rename P12 Q12_post
rename P15 Q15_post
rename P16 Q16_post
rename P18_1_resp Q18_1_post
rename P19_1_resp Q19_1_post
rename P19_2_resp Q19_2_post
rename P19_3_resp Q19_3_post
rename P20_1_resp Q20_1_post
rename P20_2_resp Q20_2_post
rename P20_3_resp Q20_3_post
rename P20_4_resp Q20_4_post
rename P22_2_resp Q22_2_post
rename P22_3_resp Q22_3_post
rename P22_5_resp Q22_5_post
rename P22_6_resp Q22_6_post
rename P19_1_resp_parti Q19_party_post
rename P25_1_resp Q25_1_post


**********Reshaping data from wide to long format to prepare for analysis************
reshape long interviewdate Q6 Q6_2 Q7 Q10 Q11 Q12 Q15 Q16 Q18_1 Q19_1 Q19_2 Q19_3 Q20_1 Q20_2 Q20_3 Q20_4 Q22_2 Q22_3 Q22_5 Q22_6 Q19_party Q25_1, i(ID) j(time) string



*Creating a time dummy variable indicating whether respondents answered before or after the campaign
codebook time
encode time, gen(time1)
codebook time1
recode time1 (1=1 "Wave 2") (2=0 "Wave 1"), gen(timedummy)
codebook timedummy


*******Creating dependent variable (vote inclinations/policy opinions)********
codebook Q7
recode Q7 (1=1 "Yes (in favour of proposal)") (2 3 88=0 "No (in opposition to proposal"), gen(vote_yes)
codebook Q7 vote_yes



**********Creating learning variable (independent variable)*********


*Perception of EU army question
codebook Q20_4
recode Q20_4 (2 88=0 "Uninformed") (1=1 "Correctly Informed"), gen(EUROPOL_perception)
codebook Q20_4 EUROPOL_perception

gen learn_policyinfo=.
sort ID timedummy
by ID (timedummy), sort: replace learn_policyinfo=0 if EUROPOL_perception[_n-1]==0 & (EUROPOL_perception==0)
by ID (timedummy), sort: replace learn_policyinfo=1 if EUROPOL_perception[_n-1]==0 & (EUROPOL_perception==1)
by ID (timedummy), sort: replace learn_policyinfo=2 if EUROPOL_perception[_n-1]==1 & (EUROPOL_perception==1)
by ID (timedummy), sort: replace learn_policyinfo=3 if EUROPOL_perception[_n-1]==1 & (EUROPOL_perception==0)
label define learn_policyinfo 0 "Never Learned" 1 "Learned Policy Info" 2 "Already Knew" 3 "Forgot"
label values learn_policyinfo learn_policyinfo
codebook learn_policyinfo

bro ID timedummy learn_policyinfo EUROPOL_perception

*Adding values to misssing observation regarding independent variable (learning)
bysort ID (timedummy) : replace learn_policyinfo = learn_policyinfo[_n+1] if missing(learn_policyinfo)

*Checking coding was made correct
bro ID timedummy EUROPOL_perception learn_policyinfo
tab timedummy learn_policyinfo, row





************************Control Variables************************



*******Time-invariant background variables - used for matching and control for unispecific time-trends********

*Gender
codebook Q2
recode Q2 (1=1 "Male") (2=2 "Female"), gen(gender)
codebook Q2 gender
tab timedummy gender, row

*Age
codebook alder, tab(99)
bro ID timedummy alder

gen age_cat=.
replace age_cat=1 if inrange(alder, 17, 29.1)
replace age_cat=2 if inrange(alder, 29.9, 39.1)
replace age_cat=3 if inrange(alder, 39.9, 49.1)
replace age_cat=4 if inrange(alder, 49.9, 59.1)
replace age_cat=5 if inrange(alder, 59.9, 69.1)
replace age_cat=6 if inrange(alder, 69.9, 120)

label define age_cat 1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60-69" 6 "70+"
label values age_cat age_cat
codebook age_cat

*Education
codebook Q4
tab timedummy Q4, row
recode Q4 (5=2 "Vocational school") (1 2=1 "Elementary school") (3 4=3 "High school") (6=4 "Higher education, short") (7=5 "Higher education, middle") (8 9=6 "Higher education, long") , gen(education)
codebook Q4 education
tab timedummy education, row

*Occupation
codebook Q5, tab(99)
recode Q5 (9 88=6 "Other") (6=3 "Uemployed") (4 5=2 "Employee in public sector") (1 2 3=1 "Employee in private sector") (7=4 "Retired") (8=5 "Student"), gen(occupation)
codebook Q5 occupation, tab(99)
tab timedummy occupation, row


*Region of residence
codebook region
recode region (1084=1 "Copenhagen") (1085=2 "Zealand") (1083=3 "South Denmark") (1082=4 "Mid-Jutland") (1081=5 "North Jutland"), gen(region1)
codebook region region1
drop region
rename region1 region
codebook region


*Partisanship - vote choice in last general election (4 months before) measured in wave 1 (used for analysis where I restrict the pool to only include voters who support a party recommending a "Yes")
codebook Q16, tab(99)
gen partisanship=Q16 if timedummy==0
bysort ID (timedummy) : replace partisanship = partisanship[_n-1] if missing(partisanship)
codebook Q16 partisanship if timedummy==0, tab(99)
recode partisanship (1 2 3 4 6 8 10=1 "Supports party recommending a YES") (5 7 9=2 "Supports party recommending a NO") (12 13 14 15 88=3 "Don't know/else"), gen(partisanship1)
codebook partisanship1 Q16 if timedummy==0, tab(99)
drop partisanship
rename partisanship1 partisanship
tab timedummy partisanship , row

*Prior ideological self-placement (used for matching)
codebook Q18_1, tab(99)
gen prior_ideology=Q18_1 if timedummy==0
bysort ID (timedummy) : replace prior_ideology = prior_ideology[_n-1] if missing(prior_ideology)
codebook Q18_1 prior_ideology if timedummy==0, tab(99)
replace prior_ideology=6 if prior_ideology==88
label define ideology 1 "Left" 11 "Right"
label values prior_ideology ideology
codebook prior_ideology, tab(99)
tab timedummy prior_ideology, row 


*Prior EU values (used for matching)
codebook Q12
gen prior_EU_values=Q12 if timedummy==0
bysort ID (timedummy) : replace prior_EU_values = prior_EU_values[_n-1] if missing(prior_EU_values)
codebook Q12 prior_EU_values if timedummy==0
recode prior_EU_values (1=5 "Clearly most benefits (of EU membership)") (2=4 "Predominantly most benefits") (3 88=3 "Neither nor (including don't knows)") (4=2 "Predominantly most disadvantages") (5=1 "Clearly most disadvantages (of EU membership)") , gen(prior_EU_values1)
codebook prior_EU_values1 Q12 if timedummy==0
drop prior_EU_values
rename prior_EU_values1 prior_EU_values
tab timedummy prior_EU_values, row



*********Time-varying control variables********


*Perceived government performance
codebook Q10
recode Q10 (1=5 "Very good") (2=4 "Good") (3 88=3 "Neither good nor bad (including don't knows)") (4=2 "Bad") (5=1 "Very bad"), gen(perceived_governmentperformance)
codebook Q10 perceived_governmentperformance

*Interest in politics regarding the EU
codebook Q6
recode Q6 (1=5 "Very interested") (2=4 "Somewhat interested") (3=3 "Neither interested nor uninterested") (4=2 "Less interested") (5=1 "Not at all interested"), gen(EU_polinterest)
codebook Q6 EU_polinterest


*Knowledge of Danish Opt-Out (index construction)
codebook Q20_1 Q20_2 Q20_3
recode Q20_1 (1=1 "Correct answer") (2 88=0 "Don't know the right answer"), gen(knowledge1)
codebook Q20_1 knowledge1
recode Q20_2 (2=1 "Correct answer") (1 88=0 "Don't know the right answer"), gen(knowledge2)
codebook Q20_2 knowledge2
recode Q20_3 (2=1 "Correct answer") (1 88=0 "Don't know the right answer"), gen(knowledge3)
codebook Q20_3 knowledge3

pwcorr knowledge1 knowledge2 knowledge3
alpha knowledge1 knowledge2 knowledge3, item min(3)
alpha knowledge1 knowledge2 knowledge3, gen(knowledge_index) min(3)
hist knowledge_index
sum knowledge_index
codebook knowledge_index, tab(99)
recode knowledge_index (0=0 "0 answers corrects") (0.33333334=1 "1 answer correct") (0.66666669=2 "2 answers correct") (1=3 "3 answers correct"), gen(knowledge)
codebook knowledge_index knowledge, tab(99)
drop knowledge_index



****Learning your party's position (vote recommendation)


*Parti-ID
codebook Q16, tab(99)

gen parti_id=.
replace parti_id=1 if Q16==1 & timedummy==0
replace parti_id=2 if Q16==2 & timedummy==0
replace parti_id=3 if Q16==3 & timedummy==0
replace parti_id=4 if Q16==4 & timedummy==0
replace parti_id=5 if Q16==5 & timedummy==0
replace parti_id=6 if Q16==6 & timedummy==0
replace parti_id=7 if Q16==7 & timedummy==0
replace parti_id=8 if Q16==8 & timedummy==0
replace parti_id=9 if Q16==9 & timedummy==0
replace parti_id=10 if Q16==10 & timedummy==0

bysort ID (timedummy) : replace parti_id = parti_id[_n-1] if missing(parti_id)


label define parti_id 1 "Socialdemokraterme" 2 "Radikale Venstre" 3 "Det Konservative Folkeparti" 4 "SF" 5 "Liberal Alliance" 6 "Kristendemokraterne" 7 "Dansk Folkeparti" 8 "Venstre" 9 "Enhedslisten" 10 "Alternativet"
label values parti_id parti_id
codebook parti_id, tab(99)
bro ID timedummy parti_id 


*Knowledge of party's positions on referendum issue

codebook Q19_1 Q19_2 Q19_3
gen know_own_partyposition=.
replace know_own_partyposition=0 if parti_id==1 & Q19_2!=1
replace know_own_partyposition=0 if parti_id==2 & Q19_1!=1
replace know_own_partyposition=0 if parti_id==3 & Q19_1!=1
replace know_own_partyposition=0 if parti_id==4 & Q19_1!=1
replace know_own_partyposition=0 if parti_id==5 & Q19_1!=2
replace know_own_partyposition=0 if parti_id==6 & Q19_1!=1
replace know_own_partyposition=0 if parti_id==7 & Q19_3!=2
replace know_own_partyposition=0 if parti_id==8 & Q19_1!=1
replace know_own_partyposition=0 if parti_id==9 & Q19_1!=2
replace know_own_partyposition=0 if parti_id==10 & Q19_1!=1

replace know_own_partyposition=1 if parti_id==1 & Q19_2==1
replace know_own_partyposition=1 if parti_id==2 & Q19_1==1
replace know_own_partyposition=1 if parti_id==3 & Q19_1==1
replace know_own_partyposition=1 if parti_id==4 & Q19_1==1
replace know_own_partyposition=1 if parti_id==5 & Q19_1==2
replace know_own_partyposition=1 if parti_id==6 & Q19_1==1
replace know_own_partyposition=1 if parti_id==7 & Q19_3==2
replace know_own_partyposition=1 if parti_id==8 & Q19_1==1
replace know_own_partyposition=1 if parti_id==9 & Q19_1==2
replace know_own_partyposition=1 if parti_id==10 & Q19_1==1


codebook know_own_partyposition
label define know_own_partyposition 0 "Don't know owns party's recommendation" 1 "Knows owns party's recommendation"
label values know_own_partyposition know_own_partyposition
codebook know_own_partyposition

gen learn_partyposition=.
sort ID timedummy
by ID (timedummy), sort: replace learn_partyposition=0 if know_own_partyposition[_n-1]==0 & (know_own_partyposition==0)
by ID (timedummy), sort: replace learn_partyposition=1 if know_own_partyposition[_n-1]==0 & (know_own_partyposition==1)
by ID (timedummy), sort: replace learn_partyposition=2 if know_own_partyposition[_n-1]==1 & (know_own_partyposition==1)
by ID (timedummy), sort: replace learn_partyposition=3 if know_own_partyposition[_n-1]==1 & (know_own_partyposition==0)
label define learn_partyposition 0 "Never Learned" 1 "Learned Party Cue" 2 "Already Knew" 3 "Forgot"
label values learn_partyposition learn_partyposition
codebook learn_partyposition

*Adding values to misssing observation regarding independent variable (learning)
bysort ID (timedummy) : replace learn_partyposition = learn_partyposition[_n+1] if missing(learn_partyposition)

bro ID timedummy know_own_partyposition learn_partyposition


*Issue attitude 1 - Will be easy to get a parallell agreement with Europol in case of a no
codebook Q22_3
recode Q22_3 (1=5 "Completely agree (that it will be easy to get a parallell agreement with Europol in case of a no)") (2=4 "Somewhat agree") (3 88=3 "Neither agree nor disagree (including don't know)") (4=2 "Somewhat disagree") (5=1 "Completely disagree (that it will be easy to get a parallell agreement with Europol in case of a no)"), gen(issueattitude1)
codebook Q22_3 issueattitude1

*Issue attitude 2 - It will be easy for a political majority to join a common EU asylum- and immigration policy in the future
codebook Q22_6
recode Q22_6 (1=5 "Completely agree (that it will be easy for a political majority to join a common EU asylum- and immigration policy in the future)") (2=4 "Somewhat agree") (3 88=3 "Neither agree nor disagree (including don't know)") (4=2 "Somewhat disagree") (5=1 "Completely disagree (that it will be easy for a political majority to join a common EU asylum- and immigration policy in the future)"), gen(issueattitude2)
codebook Q22_6 issueattitude2



*Generate variable that measures vote-change (used for matching analysis)
gen vote_yes_change=.
sort ID timedummy
by ID (timedummy), sort: replace vote_yes_change=0 if vote_yes[_n-1]==0 & (vote_yes==0)
by ID (timedummy), sort: replace vote_yes_change=0 if vote_yes[_n-1]==1 & (vote_yes==1)
by ID (timedummy), sort: replace vote_yes_change=1 if vote_yes[_n-1]==0 & (vote_yes==1)
by ID (timedummy), sort: replace vote_yes_change=-1 if vote_yes[_n-1]==1 & (vote_yes==0)
label define vote_yes_change 0 "Did not change vote" 1 "Changed to YES" -1 "Changed to NO"
label values vote_yes_change vote_yes_change
codebook vote_yes_change

*Adding values to misssing observation regarding independent variable (learning)
bysort ID (timedummy) : replace vote_yes_change = vote_yes_change[_n+1] if missing(vote_yes_change)



**************ANALYSIS****************

*Dropping respondents who "forgot" policy info
drop if learn_policyinfo==3


***********Figure 1***************
reg vote_yes i.timedummy##i.learn_policyinfo, cl(ID)
margins, over(timedummy learn_policyinfo)
marginsplot, scheme(plotplain) title("               2015 Danish Opt-Out Referendum", size(large)) subtitle({it:Policy Information: Reason for Supporting}, size(medium)) xtitle("") ytitle("Proportion Voting Yes", size(medium)) legend(ring(0) size(small) pos(0.0) order(6 "Always Knew" 5 "Learned Policy Information"  4 "Never Learned")) ylab(0(0.2)1) ylabel(0 "0%" 0.2 "20%" 0.4 "40%" 0.6 "60%" 0.8 "80%" 1 "100%", labsize(medsmall)) xscale(r(-0.2(1.4)1.2)) xlabel(0(1)1, valuelabels nogrid labsize(medsmall)) xsize(4.5) xline(2.8) ciopts(recast(rspike)) plot1opts(mfcolor(gray)lpattern("1")msym(O)msize(medium)) plot2opts(mfcolor(black)lpattern("1")msym(O)msize(medium)) plot3opts(mfcolor(white)lpattern("1")msym(O)msize(medium)) xoverhangs text(0.08197 -0.10 "8%" 0.14835 -0.09 "15%" 0.4939024 -0.095 "49%" 0.1967213  1.1 "20%" 0.3736264  1.1 "37%" 0.5914634 1.1 "59%", size(medsmall))


*Change control group to those who always knew policy info (for statistical analysis)
recode learn_policyinfo (0=.) (1=1 "Learned Policy Information") (2=0 "Already Knew"), gen(learn_policyinfo_new)
codebook learn_policyinfo learn_policyinfo_new
drop if learn_policyinfo_new==.



***All voters***

*Model 1
reg vote_yes i.timedummy##i.learn_policyinfo_new, cl(ID)

*Model 2
reg vote_yes i.timedummy##i.learn_policyinfo_new perceived_governmentperformance i.know_own_partyposition EU_polinterest knowledge issueattitude1 issueattitude2, cl(ID)

*Model 3
reg vote_yes i.timedummy##(i.learn_policyinfo_new i.gender i.age_cat i.education i.region i.occupation) perceived_governmentperformance i.know_own_partyposition EU_polinterest knowledge issueattitude1 issueattitude2, cl(ID)


****Voters whose party recommend voting yes (in favour of the referendum proposal)****

*Model 4
reg vote_yes i.timedummy##i.learn_policyinfo_new if partisanship==1, cl(ID)

*Model 5
reg vote_yes i.timedummy##i.learn_policyinfo_new perceived_governmentperformance i.know_own_partyposition EU_polinterest knowledge issueattitude1 issueattitude2 if partisanship==1, cl(ID)

*Model 6
reg vote_yes i.timedummy##(i.learn_policyinfo_new i.gender i.age_cat i.education i.region i.occupation) perceived_governmentperformance i.know_own_partyposition EU_polinterest knowledge issueattitude1 issueattitude2 if partisanship==1, cl(ID)

****Matched Group****
psmatch2 learn_policyinfo_new i.gender i.age_cat i.education i.region i.occupation prior_ideology prior_EU_values if timedummy==0, out( vote_yes_change ) n(1) noreplacement
bysort ID (timedummy) : replace _weight = _weight[_n-1] if missing(_weight)


*Model 7
reg vote_yes i.timedummy##i.learn_policyinfo_new if _weight==1, cl(ID)

*Model 8
reg vote_yes i.timedummy##i.learn_policyinfo_new perceived_governmentperformance i.know_own_partyposition EU_polinterest knowledge issueattitude1 issueattitude2 if _weight==1, cl(ID)
